let connection = require("../dbutil/dbutil");
let myModule = require("./my-module");
let evaluateDao = require("./evaluate");

function addInfo(uid, token, tid, callback) {
    let sql = "select username from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1003,
                msg: "失败"
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let osqls = "select * from `order` where uid = '"+ uid +"' AND situation = '未支付'"
                connection.query(osqls, function (err, result) {
                    if (err) {
                        console.log(err);
                    } else {
                        if (result.length > 0) {
                            ret.code = 1002,
                            ret.msg = "失败"
                            callback(ret)
                        } else {
                            let tids = tid.split("$")
                            let tidList = ""
                            for (let i = 0; i < tids.length; i++) {
                                tidList += "tid = '" + tids[i] + "'"
                                if (i < tids.length - 1)
                                    tidList += " OR "
                            }
                            let tsqls = "SELECT pid FROM trolley WHERE " + tidList;
                            connection.query(tsqls, function (err, result) {
                                if (err) {
                                    console.log(err);
                                } else {
                                    for (let i = 0; i < result.length; i++) {
                                        let osqli = "INSERT INTO `order`(uid, pid, situation) VALUES("+ uid + "," + result[i].pid + ", '未支付');"
                                        connection.query(osqli, function (err) {
                                            if (err) {
                                                console.log(err);
                                            }
                                        })
                                    }
                                    
                                    let tsqld = "DELETE FROM trolley WHERE " + tidList;
                                    connection.query(tsqld, function (err) {
                                        if (err) {
                                            console.log(err);
                                        }
                                    })
                                    ret.code = 1001,
                                    ret.msg = "成功"
                                    callback(ret)
                                }
                            })
                        }
                        
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function getInfo(uid, token, callback) {
    let sql = "select * from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                data: []
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let osql = "SELECT * FROM order_view WHERE uid = '" + uid + "'"
                connection.query(osql, function (err, result) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001
                        ret.data = result
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function confirmOrder(uid, token, oid, callback) {
    let sql = "select * from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                msg: "失败"
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let osql = "UPDATE `order` SET situation = '待评价' WHERE oid = '" + oid + "'"
                connection.query(osql, function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001
                        ret.msg = "成功"
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function evaluateOrder(uid, token, oid, text, callback) {
    let sql = "select * from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                msg: "失败"
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let osql = "UPDATE `order` SET situation = '已完成' WHERE oid = '" + oid + "'"
                connection.query(osql, function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        evaluateDao.evaluate(oid, uid, text)
                        ret.code = 1001
                        ret.msg = "成功"
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function deleteOrder(uid, token, oid, callback) {
    let sql = "select * from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                msg: "失败"
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let osql = "DELETE FROM `order` WHERE oid = '" + oid + "'"
                connection.query(osql, function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001
                        ret.msg = "成功"
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

exports.addInfo = addInfo
exports.getInfo = getInfo
exports.confirmOrder = confirmOrder
exports.evaluateOrder = evaluateOrder
exports.deleteOrder = deleteOrder